import {onMounted, reactive, ref} from 'vue';
import {deleteDb, queryIPage, queryIPageS,} from '@/api/url/erp/baseMessage';
import { sliceTime } from '@/utils/time';
import { VxeTableInstance, VxeButtonEvents } from 'vxe-table';
import router from "@/router";
import notification from "@arco-design/web-vue/es/notification";
export default function importConfig() {
  const base = reactive<any>({
    checkForm: {
      craft: "",
      itemNo: "",
      name: "",
      number: "",
      orderNumber: "",
      startTime: "",
      stopTime: "",
      workCenter:""
    },
    timeRange:[],
    pagination:{
      current: 1,
      size: 10,
      total: 1,
    },
    tableRef:null,
    data: [],
    visible: false, //遮罩层显示
    delVisible: false, //删除用户遮罩层
    loadingFlag: false,
  });

  //获取表格数据
  const getTable = () => {
    base.loadingFlag=true;
    let obj={
      current: base.pagination.current,
      size: base.pagination.size,
    };

    console.log(base.checkForm);

    let rel=Object.assign(obj,base.checkForm);

    console.log(rel);

    queryIPageS(rel).then((res: any) => {
      if(res.code === 200){
        base.pagination.total = res.data.total;
        base.data = res.data.data.map((item: any) => {
          item.TE023 = sliceTime(item.TE023).replaceAll(":","");
          item.TE001 = item.TE001 + "-" + item.TE002 + "-" + item.TE003;
          item.TE006 = item.TE006 + '-' + item.TE007;
          return item;
        });
        base.loadingFlag=false;
      }else{
        base.loadingFlag=false;
      }
    })
  };

  //查询
  const search=()=>{
    base.pagination.current=1;
    getTable();
  };

  // 重置
  const reset = () => {
    base.checkForm=JSON.parse(JSON.stringify(base.emptyCheckForm));
    getTable();
  };

  // 导出
  const outExcleFun:  VxeButtonEvents.Click = () => {
    const $table = base.tableRef;
    if($table){
      $table.exportData({ type: 'xlsx' })
    }
  };
  //存储查询条件
  const storeCondition=()=>{
    localStorage.setItem('dispatchQueryFormData',JSON.stringify(base.checkForm));
  };

  // 跳到详情页
  const toDetail = (row) => {
    storeCondition();

    router.push({
      path: '/querySfCteModel',
      query: { gx: row.TE008.trim(), odd: row.TE001.split('-')[1], single:row.TE001.split('-')[0],source:4 },
    });
  };

  //打开删除弹窗
  const del=(row)=>{
    base.currentInfo=row;
    base.delVisible=true;
  };

  // 确定删除
  const delHandleOk = () => {
    console.log(base.currentInfo);
    let obj={
      single:base.currentInfo.TE001.split('-')[0],
      odd:base.currentInfo.TE002,
      xh:base.currentInfo.TE003
    };

    deleteDb(obj).then(res=>{
      notification.info(res.data);
      getTable();
    });
  };

  //改变时间
  const changeTime=(value)=>{
    if(value){
      base.checkForm.startTime=value[0];
      base.checkForm.stopTime=value[1];
    }else{
      base.checkForm.startTime='';
      base.checkForm.stopTime='';
    }
  };

  onMounted(()=>{
    base.emptyCheckForm=JSON.parse(JSON.stringify(base.checkForm));
    let relt=JSON.parse(localStorage.getItem('dispatchQueryFormData'));
    if(relt){
      base.checkForm=relt;
      base.timeRange=[relt.startTime,relt.stopTime];
    }
    getTable();
  });

  return {
    base,
    getTable,
    reset,
    outExcleFun,
    toDetail,
    del,
    delHandleOk,
    search,
    changeTime,
  };
}
